package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;

import cpcns.jhsks.util.MapCollectionUtils;

import com.younion.business.IProductKindService;
import com.younion.vo.CustomerVO;
import com.younion.vo.ProductKindVO;

/**
 * @author wells
*/
public class ProductKindAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(ProductKindAction.class);

	private IProductKindService productKindService = null;

	private ProductKindVO productKindVO = new ProductKindVO();

	private List<ProductKindVO> productKindList;// 结果集列表
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("listProductKind")
	public String listProductKind() {
		try {
			Map paramMap = null;
			if(getSessionUser().getCustomer() != null){
				productKindVO.setCustomerVO(getSessionUser().getCustomer());
			}else if(productKindVO!=null && productKindVO.getCustomerId() != null){
				productKindVO.setCustomerVO(new CustomerVO(productKindVO.getCustomerId()));
			}
			if (productKindVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(productKindVO));
			}
			//获取总记录数
			int total = productKindService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			productKindList = productKindService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
			this.addLog(2, "查询商品分类列表", "查询商品分类列表", this.getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("addProductKind")
	public String addProductKind() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	@RequiresPermissions("editProductKind")
	public String editProductKind() {
		try {
			productKindVO = productKindService.getById(productKindVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 *保存动作
	 */
	@RequiresPermissions("saveProductKind")
	public String saveProductKind() {
		try {
			productKindVO = productKindService.save(productKindVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "productKindList");
		String id = productKindVO.getId() == null ? "" : productKindVO.getId().toString();
		String log = "".equals(id) ? "添加商品分类" : "更新商品分类";
		int type = "".equals(id) ? 3:4;
		this.addLog(type, log+id, log,getSessionUser());
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("deleteProductKind")
	public String deleteProductKind() {
		try {
			String pkId = request.getParameter("ids");
			productKindService.remove(Arrays.asList(pkId.split(",")));
			String log = "删除商品分类";
			this.addLog(5, log+pkId, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "productKindList");
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	@RequiresPermissions("viewProductKind")
	public String viewProductKind() {
		try {
			String pkId = request.getParameter("ids"); 
			productKindVO = productKindService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public ProductKindVO getProductKindVO() {
		return productKindVO;
	}

	public void setProductKindVO(ProductKindVO productKindVO) {
		this.productKindVO = productKindVO;
	}

	public void setProductKindService(IProductKindService productKindService) {
		this.productKindService = productKindService;
	}

	public List<ProductKindVO> getProductKindList() {
		return productKindList;
	}

	public void setProductKindList(List<ProductKindVO> productKindList) {
		this.productKindList = productKindList;
	}

}